Welcome to pandas!

8.7 表格纵向拼接(初级)

工作中经常会遇到将多个表格的数据合并到一个表格,这种操作叫拼接,拼接分为纵向拼接与模向拼接,有四种方法:append、concat、join、merge这4种拼接函数,不同函数支持不同的拼接方式。

拼接方式 append concat join merge
纵向拼接
横向拼接

1、 需要使用将两个表格或者更新表格的记录拼接合并,则可以使用df.append()函数,如构如下:

df.append(other,ignore_index=False,verify_integrity=False,sort=False)

other :被合并的对象,可以是Series、字典、DateFrame,如果添加多个可将这些对象置在列表中;

ignore_index :是否重新编号;

verify_integrity :是否检测合并后行索引有重复值

sort :合并后的列索引是否排序


已经没有df.appene这个函数了,可以使用df._append

import pandas as pd

df=pd.read_excel(r "D:\Pyobject2023\object\测试\素材\1.8.07 表格纵向拼接(初级).xlsx" )

print (df)

l=[

pd.Series({ "姓名":"小韦","工资":"888" }),

pd.Series({ "姓名": "许流子", "工资": "777" }),

pd.Series({ "姓名": "郭鬼子", "工资": "444" })

]

df1=df._append(

other = l ,

ignore_index = True )

print (df1)

返回:

姓名 工资
0 张三 111
1 李四 222
2 王麻子 333

姓名 工资
0 张三 111
1 李四 222
2 王麻子 333
3 小韦 888
4 许流子 777
5 郭鬼子 444

import pandas as pd

cg=pd.read_excel(r "D:\Pyobject2023\object\测试\素材\1.8.07 表格纵向拼接(初级).xlsx","采购部")

cw=pd.read_excel(r "D:\Pyobject2023\object\测试\素材\1.8.07 表格纵向拼接(初级).xlsx","财务部")

sc=pd.read_excel(r "D:\Pyobject2023\object\测试\素材\1.8.07 表格纵向拼接(初级).xlsx","生产部")

print (cg)

print (cw)

print (sc)

df=pd.DataFrame()._append(other=[cg,cw,sc], ignore_index = True )

print (df)

姓名 工资
0 张三 111
1 李四 222
2 王麻子 333

姓名 工资
0 小韦 888
1 小李 777
2 小余 666

姓名 工资
0 小许 333
1 小郭 444
2 小张 555

姓名 工资
0 张三 111
1 李四 222
2 王麻子 333
3 小韦 888
4 小李 777
5 小余 666
6 小许 333
7 小郭 444
8 小张 555